//
//  OtherInfoViewController.m
//  MyLittleBrother
//
//  Created by speeda on 17/8/1.
//  Copyright © 2017年 speeda. All rights reserved.
//

#import "OtherInfoViewController.h"
#import "HomeCell.h"
#import "ArticleModel.h"
#import <SDWebImage/UIButton+WebCache.h>
#import "DetailsViewController.h"

@interface OtherInfoViewController () <UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UIButton *headButton;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *cityLabel;
@property (weak, nonatomic) IBOutlet UIButton *boxNemLabel;
@property (weak, nonatomic) IBOutlet UITableView *otherInfoTableView;
@property (nonatomic, strong) NSMutableArray *otherInfoArray;

@property (nonatomic, assign) NSInteger page;

@end

@implementation OtherInfoViewController

- (void)viewWillAppear:(BOOL)animated{
    
    //设置导航栏背景图片为一个空的image，这样就透明了
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    
    //去掉透明后导航栏下边的黑边
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    
    //设置导航条主体颜色
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTableView];
    
    [self requestGetuser];
    
}


- (void)initTableView {

    self.otherInfoTableView.dataSource = self;
    self.otherInfoTableView.delegate = self;
    self.otherInfoTableView.estimatedRowHeight = 100;
    self.otherInfoTableView.rowHeight = UITableViewAutomaticDimension;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    __weak typeof(self) weakSelf = self;
    //默认block方法：设置下拉刷新
    self.otherInfoTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf AFNNewData];
    }];
    [self.otherInfoTableView.mj_header beginRefreshing];
    
    //默认block方法：设置上拉加载更多
    self.otherInfoTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf AFNMoreData];
    }];
}

#pragma mark -- 获取用户信息
- (void)requestGetuser {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"command"] = @"getuser";
    dict[@"userid"] = self.b_userid;

    [AFHTTPSTool POSTRequestRequestWebsite:Url AndDictionary:dict success:^(NSURLSessionDataTask *dataTask, id responseObject) {
        
        NSDictionary *dic = responseObject;
        NSString *state = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([state isEqualToString:@"000000"]) {
            
            NSLog(@"获取用户信息 = %@",responseObject);
            [self.headButton sd_setBackgroundImageWithURL:[NSURL URLWithString:[dic[@"data"] objectForKey:@"b_headimg"]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"img_touxiang"]];
            self.nameLabel.text = [dic[@"data"] objectForKey:@"b_nickname"];
            self.cityLabel.text = [dic[@"data"] objectForKey:@"b_city"];
            
        } else {
            
            NSString *errorText = [NSString stringWithFormat:@"%@",dic[@"msg"]];
            [SVProgressHUD showInfoWithStatus:errorText];
        }
        
        
    } failure:^(NSURLSessionDataTask *dataTask, NSError *error) {
        
        [SVProgressHUD showErrorWithStatus:error.localizedDescription];
    }];
}


#pragma mark -- 获取贴子
- (void)AFNNewData {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"command"] = @"getarticle";
    dict[@"userid"] = self.b_userid;
    dict[@"topicid"] = @"";
    dict[@"circleid"] = @"";
    dict[@"page"] = @"1";
    
    [AFHTTPSTool POSTRequestRequestWebsite:Url AndDictionary:dict success:^(NSURLSessionDataTask *dataTask, id responseObject) {
        
        [self.otherInfoTableView.mj_header endRefreshing];
        
        NSDictionary *dic = responseObject;
        NSString *state = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([state isEqualToString:@"000000"]) {
            
            NSLog(@"获取贴子 = %@",responseObject);
            [self.otherInfoArray removeAllObjects];
            
            NSArray *array = [ArticleModel mj_objectArrayWithKeyValuesArray:[dic[@"data"] objectForKey:@"rows"]];
            
            NSString *pageString = [dic[@"data"] objectForKey:@"page"];
            _page = pageString.integerValue;
            _page++;
            
            [self.otherInfoArray addObjectsFromArray:array];
            
            [self.otherInfoTableView reloadData];
            
        } else {
            
            NSString *errorText = [NSString stringWithFormat:@"%@",dic[@"msg"]];
            [SVProgressHUD showInfoWithStatus:errorText];
        }
        
        
    } failure:^(NSURLSessionDataTask *dataTask, NSError *error) {
        
        [self.otherInfoTableView.mj_header endRefreshing];
        [SVProgressHUD showErrorWithStatus:error.localizedDescription];
    }];
}


- (void)AFNMoreData {
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"command"] = @"getarticle";
    dict[@"userid"] = self.b_userid;
    dict[@"topicid"] = @"";
    dict[@"circleid"] = @"";
    dict[@"page"] = @(_page);
    
    [AFHTTPSTool POSTRequestRequestWebsite:Url AndDictionary:dict success:^(NSURLSessionDataTask *dataTask, id responseObject) {
        
        [self.otherInfoTableView.mj_footer endRefreshing];
        
        NSDictionary *dic = responseObject;
        NSString *state = [NSString stringWithFormat:@"%@",dic[@"code"]];
        if ([state isEqualToString:@"000000"]) {
            
            NSLog(@"获取贴子 = %@",responseObject);
            NSArray *array = [ArticleModel mj_objectArrayWithKeyValuesArray:[dic[@"data"] objectForKey:@"rows"]];
            
            NSString *pageString = [dic[@"data"] objectForKey:@"page"];
            _page = pageString.integerValue;
            _page++;
            
            [self.otherInfoArray addObjectsFromArray:array];
            
            [self.otherInfoTableView reloadData];
            
        } else {
            
            NSString *errorText = [NSString stringWithFormat:@"%@",dic[@"msg"]];
            [SVProgressHUD showInfoWithStatus:errorText];
        }
        
        
    } failure:^(NSURLSessionDataTask *dataTask, NSError *error) {
        
        [self.otherInfoTableView.mj_footer endRefreshing];
        [SVProgressHUD showErrorWithStatus:error.localizedDescription];
    }];
}

#pragma maek -- UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.otherInfoArray.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"articleTableViewcellID";
    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"HomeCell" owner:nil options:nil];
        cell = [nibs firstObject];
    }
    ArticleModel *articleItem = self.otherInfoArray[indexPath.row];
    cell.articleItem = articleItem;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ArticleModel *articleItem = self.otherInfoArray[indexPath.row];
    DetailsViewController *detailsVc = [[DetailsViewController alloc] init];
    detailsVc.circleid = articleItem.b_id;
    [self.navigationController pushViewController:detailsVc animated:YES];
}


- (NSMutableArray *)otherInfoArray {
    
    if (!_otherInfoArray) {
        _otherInfoArray = [NSMutableArray array];
    }
    
    return _otherInfoArray;
}


- (void)viewWillDisappear:(BOOL)animated{
    
    //    如果不想让其他页面的导航栏变为透明 需要重置
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
    
    [self.navigationController.navigationBar setTintColor:nil];
    
    
}

@end
